package com.mc.fil.service.impl;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilMultimedia;
import com.mc.fil.domain.FilWatering;
import com.mc.fil.mapper.FilMultimediaMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilDiseasesInsectPestsMapper;
import com.mc.fil.domain.FilDiseasesInsectPests;
import com.mc.fil.service.IFilDiseasesInsectPestsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 病虫害 除草情况Service业务层处理
 *
 * @author mc
 * @date 2021-04-15
 */
@Service
public class FilDiseasesInsectPestsServiceImpl implements IFilDiseasesInsectPestsService
{
    @Autowired
    private FilDiseasesInsectPestsMapper filDiseasesInsectPestsMapper;
    @Autowired
    private FilMultimediaMapper multimediaMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询病虫害 除草情况
     *
     * @param id 病虫害 除草情况ID
     * @return 病虫害 除草情况
     */
    @Override
    public FilDiseasesInsectPests selectFilDiseasesInsectPestsById(Long id) {
        FilDiseasesInsectPests diseasesInsectPests = filDiseasesInsectPestsMapper.selectFilDiseasesInsectPestsById(id);
        FilMultimedia multimedia = new FilMultimedia();
        multimedia.setFilId(id);
        multimedia.setGenre("filDiseasesInsectPests");
        diseasesInsectPests.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        return diseasesInsectPests;
    }
    /**
     * 查询病虫害 除草情况列表
     *
     * @param filDiseasesInsectPests 病虫害 除草情况
     * @return 病虫害 除草情况
     */
    @Override
    public List<FilDiseasesInsectPests> selectFilDiseasesInsectPestsList(FilDiseasesInsectPests filDiseasesInsectPests)
    {
        List<FilDiseasesInsectPests> list = filDiseasesInsectPestsMapper.selectFilDiseasesInsectPestsList(filDiseasesInsectPests);

        for(FilDiseasesInsectPests fil : list){
            FilMultimedia multimedia = new FilMultimedia();
            multimedia.setFilId(fil.getId());
            multimedia.setGenre("filDiseasesInsectPests");
            fil.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        }
        return list;
    }

    /**
     * 新增病虫害 除草情况
     *
     * @param filDiseasesInsectPests 病虫害 除草情况
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFilDiseasesInsectPests(FilDiseasesInsectPests filDiseasesInsectPests)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filDiseasesInsectPests.setCreateTime(DateUtils.getNowDate());
        filDiseasesInsectPests.setCreateUserId(loginUser.getUsername());
        filDiseasesInsectPestsMapper.insertFilDiseasesInsectPests(filDiseasesInsectPests);
        List<FilMultimedia> list = filDiseasesInsectPests.getPhotos();
        for(FilMultimedia fil : list){
            fil.setFilId(filDiseasesInsectPests.getId());
            fil.setCreatedBy(loginUser.getUsername());
            fil.setCreatedTime(DateUtils.getNowDate());
            fil.setGenre("filDiseasesInsectPests");
            multimediaMapper.insertFilMultimedia(fil);
        }
        return 1;
    }

    @Override
    public int updateFilDiseasesInsectPests(FilDiseasesInsectPests filDiseasesInsectPests) {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filDiseasesInsectPests.setCreateTime(DateUtils.getNowDate());
        filDiseasesInsectPests.setCreateUserId(loginUser.getUsername());
        List<FilMultimedia> list = filDiseasesInsectPests.getPhotos();
        for(FilMultimedia fil : list){
            fil.setFilId(filDiseasesInsectPests.getId());
            fil.setCreatedBy(loginUser.getUsername());
            fil.setCreatedTime(DateUtils.getNowDate());
            fil.setGenre("filDiseasesInsectPests");
            multimediaMapper.insertFilMultimedia(fil);
        }
        return filDiseasesInsectPestsMapper.updateFilDiseasesInsectPests(filDiseasesInsectPests);
    }

}